package cn.moon.client.handler;

import cn.moon.client.channel.ClientChannelManager;
import cn.moon.protocol.ProxyMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;


/**
 * 本地处理器
 * 负责转发请求
 */
@Slf4j
public class LocalHandler extends SimpleChannelInboundHandler<byte[]> {

    private final String channelId;

    public LocalHandler(String channelId){
        this.channelId = channelId;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, byte[] data) throws Exception {

        log.debug("======== LocalHandler 收到消息, 向服务端回传 ========");

        ProxyMessage proxyMessage = new ProxyMessage();

        proxyMessage.setType(ProxyMessage.FORWARD);
        proxyMessage.setChannelId(channelId);
        proxyMessage.setData(data);

        ClientChannelManager.clientChannelForward(proxyMessage);

    }



}
